import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;


public class PTableXmlParser {
    private static final String ns = null;
	public static class Element{
		public String name;
		public String picture;
		public String sound;
		public String symbol;
		public int number;
		public float mass;
		public float bp;
		public float mp;
		private Element(String nm, String pic, String snd, String sym, int num, float m, float b, float p){
			this.name = nm;
			this.picture = pic;
			this.sound = snd;
			this.symbol = sym;
			this.number = num;
			this.mass = m;
			this.bp = b;
			this.mp = p;
		}
	}
    public List parse(String FileName) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			FileInputStream in = new FileInputStream(FileName);
			try{
				parser.setInput(in, null);
				parser.nextTag();
				return readFile(parser);
			} finally {
				in.close();
			}
        }finally{
        }
    }
	private List readFile(XmlPullParser parser) throws XmlPullParserException, IOException {
    List entries = new ArrayList();

    parser.require(XmlPullParser.START_TAG, ns, "PTABLE");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        // Starts by looking for the entry tag
        if (name.equals("Element")) {
            entries.add(readEntry(parser));
        } else {
            skip(parser);
        }
    }  
    return entries;
}

private Element readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, "Element");
    String elemName = null;
    String pic = null;
	String sound = null;
	String sym = null;
	int number = 0;
	float bp = 0;
	float mp = 0;
	float mass = 0;
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("Name")) {
            elemName = readElemName(parser);
        } else if (name.equals("Mass")) {
            mass = readMass(parser);
        } else if (name.equals("Symbol")) {
            sym = readSym(parser);
        } else if (name.equals("Graphic")) {
            pic = readPic(parser);
        } else if (name.equals("Audio")) {
            sound = readSound(parser);
        } else if (name.equals("BoilingPoint")) {
            bp = readBP(parser);
        } else if (name.equals("AtomicNumber")) {
            number = readNum(parser);
        } else if (name.equals("MeltingPoint")) {
            mp = readMP(parser);
        } else {
            skip(parser);
        }
    }
    return new Element(elemName, pic, sound, sym, number, mass, bp, mp);
}
private String readElemName(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "Name");
    String s = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "Name");
    return s;
}
private String readPic(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "Graphic");
    String s = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "Graphic");
    return s;
}
private String readSound(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "Audio");
    String s = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "Audio");
    return s;
}
private String readSym(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "Symbol");
    String s = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "Symbol");
    return s;
}
private int readNum(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "AtomicNumber");
    String s = readText(parser);
	int i = Integer.parseInt(s);
    parser.require(XmlPullParser.END_TAG, ns, "AtomicNumber");
    return i;
}
private float readMass(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "AvgMass");
    String s = readText(parser);
	float f = Float.parseFloat(s);
    parser.require(XmlPullParser.END_TAG, ns, "AvgMass");
    return f;
}
private float readBP(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "BoilingPoint");
    String s = readText(parser);
	float f = Float.parseFloat(s);
    parser.require(XmlPullParser.END_TAG, ns, "BoilingPoint");
    return f;
}
private float readMP(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "MeltingPoint");
    String s = readText(parser);
	float f = Float.parseFloat(s);
    parser.require(XmlPullParser.END_TAG, ns, "MeltingPoint");
    return f;
}

private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
    if (parser.getEventType() != XmlPullParser.START_TAG) {
        throw new IllegalStateException();
    }
    int depth = 1;
    while (depth != 0) {
        switch (parser.next()) {
        case XmlPullParser.END_TAG:
            depth--;
            break;
        case XmlPullParser.START_TAG:
            depth++;
            break;
        }
    }
 }
 private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
    String result = "";
    if (parser.next() == XmlPullParser.TEXT) {
        result = parser.getText();
        parser.nextTag();
    }
    return result;
}
 
}